package lacus.wd_cinema.controller.views;

import jakarta.servlet.http.HttpServletRequest;
import lacus.wd_cinema.entity.Screening;
import lacus.wd_cinema.entity.User;
import lacus.wd_cinema.service.AuthService;
import lacus.wd_cinema.service.ScreeningService;
import lacus.wd_cinema.service.TicketService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 普通用户界面接口控制类
 * @author lacus
 * @version 0.6
 * @since 0.6
 */
@Controller
public class UserViewController {

    private final AuthService authService;
    private final TicketService ticketService;
    private final ScreeningService screeningService;

    public UserViewController(AuthService authService, TicketService ticketService, ScreeningService screeningService) {
        this.authService = authService;
        this.ticketService = ticketService;
        this.screeningService = screeningService;
    }

    @GetMapping
    public String root() {
        return "redirect:/login";
    }

    @GetMapping("/user")
    public String getUserHomePage() {
        return "redirect:/showing";
    }

    @GetMapping("/me")
    public String getUserPage(
            Model model,
            HttpServletRequest servletRequest) {
        model.addAttribute("user",
                authService.getCurrentUser(servletRequest));
        return "user/UserPage";
    }

    @GetMapping("/showing")
    public String getShowingPage(
            Model model,
            HttpServletRequest servletRequest) {
        model.addAttribute("user",
                authService.getCurrentUser(servletRequest));
        model.addAttribute("groups",
                screeningService.searchUpcomingScreenings());
        return "user/ShowingPage";
    }

    @GetMapping("/my-tickets")
    public String getMyTicketsPage(
            Model model,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "all") String status,
            HttpServletRequest servletRequest) {
        User user = authService.getCurrentUser(servletRequest);
        model.addAttribute("user", user);
        model.addAttribute("page", page);
        model.addAttribute("size", size);
        model.addAttribute("status", status);
        model.addAttribute("tickets",
                ticketService.getTickets(user, page, size, status));
        return "user/MyTicketsPage";
    }

    @GetMapping("/buy-ticket")
    public String getBuyTicketPage(
            Model model,
            @RequestParam String screeningId,
            HttpServletRequest servletRequest) {
        Screening screening = screeningService.getScreeningById(screeningId);
        model.addAttribute("user", authService.getCurrentUser(servletRequest));
        model.addAttribute("screening", screening);
        model.addAttribute("seats",
                screeningService.bookedSeats(screeningId));
        model.addAttribute("rows", screening.getHall().getRows());
        model.addAttribute("cols", screening.getHall().getCols());
        return "user/BuyTicketPage";
    }
}
